<?php
// 后台用户模块
class CommodityInfoAction extends CommonAction {
	private static $nodeList = array();
	private $TreeFolderHTML  = '';
	function _filter(&$map) {
		// $map['id'] = array('egt',2);
		// $map['account'] = array('like',"%".$_POST['account']."%");
	}

	public function categoryEdit() {
		$category_type = $_REQUEST['category_type'];
		$commodity_id  = $_REQUEST['commodity_id'];
		if (!empty($category_type)) {
			$model = M('CategoryInfo');
			if (!empty($_REQUEST['active'])) {
				self::$nodeList = $this->treeBuild($model->select(), false);
			} else {
				self::$nodeList = $this->treeBuild($model->select());
			}
			$CommodityCategoryModel = M('CommodityCategory');
			$where['commodity_id'] = $commodity_id;
			$info                  = $CommodityCategoryModel->where($where)->select();
			$hitlist               = array();
			foreach ($info as $key => $value) {
				$hitlist[] = $value['category_id'];
			}
			$this->TreeFolderHTML = '';
			$this->createTreeFolderHTML(self::$nodeList[0], $category_type, $hitlist);
			$this->assign('TreeFolderHTML', $this->TreeFolderHTML);
		}
		$this->assign('category_type', $category_type);
		$this->assign('commodity_id', $commodity_id);
		$this->assign('APP_CATEGORY', C('APP_CATEGORY'));
		$this->display();
	}

	public function categoryUpdate() {
		$commodity_id  = $_REQUEST['commodity_id'];
		$category_type = $_REQUEST['category_type'];
		$category_ids  = $_REQUEST['perm'];
		if (empty($commodity_id) || empty($category_type) || empty($category_ids)) {
			$this->error('参数错误');
		}
		$model                 = M('CommodityCategory');
		$data                  = array();
		$data['commodity_id']  = $commodity_id;
		$data['category_type'] = $category_type;
		$info                  = $model->where($data)->select();
		//删除
		$exist_arr = array();
		foreach ($info as $k => $v) {
			$exist_arr[] = $v['category_id'];
		}
		$add_arr    = array_diff($category_ids, $exist_arr);
		$delete_arr = array_diff($exist_arr, $category_ids);
		if (!empty($add_arr)) {
			$where['category_id'] = array('in', $add_arr);
			foreach ($add_arr as $k => $v) {
				$data['commodity_id']  = $commodity_id;
				$data['category_type'] = $category_type;
				$data['category_id']   = $v;
				$model->add($data);
			}
		}
		if (!empty($delete_arr)) {
			$where['commodity_id']  = $commodity_id;
			$where['category_type'] = $category_type;
			$where['category_id']   = array('in', $delete_arr);
			$model->where($where)->delete();
		}
		$this->success('设置成功');
	}

	protected function treeBuild($data, $active = true) {
		$nodeList                     = array();
		$rootnode                     = new stdClass();
		$rootnode->parent             = null;
		$rootnode->children           = array();
		$rootnode->self->categoryId   = 0;
		$rootnode->self->categoryName = '根节点';
		$rootnode->self->level        = 0;
		$nodeList[]                   = $rootnode;
		foreach ($data as $key => $value) {
			if ($active && $value['status'] == 10) {
				continue;
			}
			if (!isset($nodeList[$value['category_id']])) {
				$nodeList[$value['category_id']] = new stdClass();
			}
			if (!isset($nodeList[$value['up_category_id']])) {
				self::$nodeList[$value['up_category_id']] = new stdClass();
			}
			if (!isset($nodeList[$value['up_category_id']]->children)) {
				$nodeList[$value['up_category_id']]->children = array();
			}
			$nodeList[$value['category_id']]->self->categoryId   = $value['category_id'];
			$nodeList[$value['category_id']]->self->categoryName = $value['category_name'];
			$nodeList[$value['category_id']]->self->level        = $value['level'];
			$nodeList[$value['category_id']]->self->categoryType = $value['category_type'];
			$nodeList[$value['category_id']]->parent             = $value['up_category_id'];
			$nodeList[$value['up_category_id']]->children[]      = $value['category_id'];
		}
		return $nodeList;
	}

	protected function createTreeFolderHTML($node, $category_type, $hitlist) {
		if ($node->self->categoryType != $category_type && $node->self->categoryId != 0) {
			return false;
		}
		if (!isset($node->self->categoryId) || !isset($node->self->categoryName)) {
			return false;
		}
		if (in_array($node->self->categoryId, $hitlist)) {
			$this->TreeFolderHTML .= '<li class="folder"><a checked="true" tname="perm[]" tvalue="' . $node->self->categoryId . '">' . $node->self->categoryName . '</a>';
		} else {
			$this->TreeFolderHTML .= '<li class="folder"><a tname="perm[]" tvalue="' . $node->self->categoryId . '">' . $node->self->categoryName . '</a>';
		}
		if (empty($node->children)) {
			return false;
		} else {
			$this->TreeFolderHTML .= '<ul>';
			foreach ($node->children as $k => $id) {
				$this->createTreeFolderHTML(self::$nodeList[$id], $category_type, $hitlist);
			}
			$this->TreeFolderHTML .= '</ul>';
		}
		$this->TreeFolderHTML .= '</li>';
		return true;
	}

	protected function _search($name = '') {
		//生成查询条件
		$name  = $this->getActionName();
		$model = D($name);
		$map   = array();
		foreach ($model->getDbFields() as $key => $val) {
			if (isset($_REQUEST[$val]) && $_REQUEST[$val] != '') {
				$map[$val] = $_REQUEST[$val];
			}
		}
		if (empty($map['is_cancle'])) {
			$map['is_cancle'] = 0;
		}
		return $map;
	}
	
	public function picEdit(){
		$commodity_id = $_REQUEST['commodity_id'];
		$type = $_REQUEST['type'];
		$commodityPicInfo = D('CommodityPicInfo');
		$pic_list = $commodityPicInfo->where("commodity_id = '".$commodity_id."' and type = '".$type."'")->select();
		$this->assign('pic_list', $pic_list);
		$this->assign('type', $type);
		$this->assign('commodity_id', $commodity_id);
		$this->display();
	}
	
	public function picSet(){
		// dump($_REQUEST);exit;
		if(empty($_REQUEST['brief_pic'])){
			$this->error("图片地址为空");
		}
		$commodityPicInfo = D('CommodityPicInfo');
		foreach($_REQUEST['brief_pic'] as $k=>$brief_pic){
			if(!empty($_REQUEST['commodity_pic_id'][$k])){//update
				if(!empty($brief_pic)){
					$commodityPicInfo->where("commodity_pic_id = '".$_REQUEST['commodity_pic_id'][$k]."'")->save(array("brief_pic"=>$brief_pic));
				}else{
					$commodityPicInfo->where("commodity_pic_id = '".$_REQUEST['commodity_pic_id'][$k]."'")->delete();
				}
				
			}else{//insert
				if(!empty($brief_pic)){
					$iData = array();
					$iData['commodity_id'] = $_REQUEST['commodity_id'];
					$iData['type'] = $_REQUEST['type'];
					$iData['brief_pic'] = $brief_pic;
					$commodityPicInfo->data($iData)->add();
				}
			}
		}
		$this->success("操作成功");
	}
	// 插入数据
	public function insert() {
		// dump($_REQUEST);exit;
		$data['commodity_id'] = $_REQUEST['commodity_id'];
		$data['name']         = $_REQUEST['name'];
		$data['sale_cycle']   = $_REQUEST['sale_cycle'];
		$data['brief_desc']   = $_REQUEST['brief_desc'];
		$data['desc']         = $_REQUEST['desc'];
		$data['spec']         = $_REQUEST['spec'];
		$data['origin']       = $_REQUEST['origin'];
		$data['unit_id']      = $_REQUEST['unit_id'];
		$data['supply_begin'] = empty($_REQUEST['supply_begin'])?null:$_REQUEST['supply_begin'];
		$data['supply_end']   = empty($_REQUEST['supply_end'])?null:$_REQUEST['supply_end'];
		$data['market_price'] = $_REQUEST['market_price'];
		$data['sale_price']   = $_REQUEST['sale_price'];
		$data['sort']         = $_REQUEST['sort'];
		$data['is_onsale']    = 0;
		$data['is_cancle']    = 0;
		$commodityInfo        = D('CommodityInfo');
		$commodityInfo->data($data)->add();
		$this->success('商品添加成功！');
	}

	function edit() {
		$name  = $this->getActionName();
		$model = M($name);
		$id    = $_REQUEST[$model->getPk()];
		$vo    = $model->where("commodity_id='" . $id . "'")->find();
		$this->assign('vo', $vo);
		$this->display();
	}

	public function update() {
		$data['commodity_id'] = $_REQUEST['commodity_id'];
		$data['name']         = $_REQUEST['name'];
		$data['sale_cycle']   = $_REQUEST['sale_cycle'];
		$data['brief_desc']   = $_REQUEST['brief_desc'];
		$data['desc']         = $_REQUEST['desc'];
		$data['spec']         = $_REQUEST['spec'];
		$data['origin']       = $_REQUEST['origin'];
		$data['unit_id']      = $_REQUEST['unit_id'];
		$data['supply_begin'] = empty($_REQUEST['supply_begin'])?null:$_REQUEST['supply_begin'];
		$data['supply_end']   = empty($_REQUEST['supply_end'])?null:$_REQUEST['supply_end'];
		$data['market_price'] = $_REQUEST['market_price'];
		$data['sale_price']   = $_REQUEST['sale_price'];
		$data['sort']         = $_REQUEST['sort'];
		// $data['is_onsale']    = 0;
		// $data['is_cancle']    = 0;
		$commodityInfo        = D('CommodityInfo');
		$commodityInfo->where("commodity_id = '" . $data['commodity_id'] . "'")->save($data);
		$this->success('商品修改成功！');
	}

	// 下架
	public function offSale() {
		$commodityInfo     = D('CommodityInfo');
		$commodity_id      = $_REQUEST['commodity_id'];
		$data['is_onsale'] = 0;
		$commodityInfo->where("commodity_id='" . $commodity_id . "'")->save($data);
		$this->ajaxReturn('', "下架成功!", 1); //刷新页面
	}

	// 上架
	public function onSale() {
		$commodityInfo     = D('CommodityInfo');
		$commodity_id      = $_REQUEST['commodity_id'];
		$data['is_onsale'] = 1;
		$commodityInfo->where("commodity_id='" . $commodity_id . "'")->save($data);
		$this->ajaxReturn('', "上架成功!", 1); //刷新页面
	}

	// 废除该商品
	public function doCancle() {
		$commodityInfo     = D('CommodityInfo');
		$commodity_id      = $_REQUEST['commodity_id'];
		$data['is_cancle'] = 1;
		$commodityInfo->where("commodity_id='" . $commodity_id . "'")->save($data);
		$this->ajaxReturn('', "废除成功!", 1); //刷新页面
	}
}
?>